﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Catering.Catering_Page.system
{
    public partial class AutoRecipeSet : System.Web.UI.Page
    {
        private DataTable dt_DishType = new DataTable(); //菜品分类列表

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadData();
            }
        }
        
        //加载页面数据
        public void LoadData()
        {
            DataTable dt_FoodMealType = new Catering_Rules.FoodMealType().GetList(" FMName<>'上午茶' AND FMName<>'下午茶'").Tables[0];
            this.Drop_Food.LoadDrop(dt_FoodMealType, "id", "FMName");

            dt_DishType = new Catering_Rules.DishType().GetAllList(false).Tables[0];
            this.Drop_DishType.Items.Clear();
            DataRow[] RowList = dt_DishType.Select("ParentID=0");
            this.LoadDrop(RowList);

            this.Drop_TypeNum.Items.Clear();
            for (int i = 1; i <= 20; i++)
            {
                ListItem item = new ListItem();
                item.Value = i.ToString();
                item.Text = i.ToString();
                this.Drop_TypeNum.Items.Add(item);
            }

            this.LoadRep();
        }

        //绑定菜品分类
        private void LoadDrop(DataRow[] RowList)
        {
            foreach (DataRow row in RowList)
            {
                ListItem ite = new ListItem();
                string Temp = "";
                int Level = Convert.ToInt32(row["TypeLevel"].ToString());
                if (Level > 0)
                {
                    for (int i = 0; i < Level; i++)
                    {
                        Temp += "┣";
                    }
                }
                ite.Text = Temp + row["DT"].ToString();
                ite.Value = row["id"].ToString();
                this.Drop_DishType.Items.Add(ite);
                DataRow[] TempList = dt_DishType.Select("ParentID=" + row["id"].ToString());
                if (TempList != null && TempList.Count() > 0)
                {
                    this.LoadDrop(TempList);
                }
            }
        }

        //加载数据列表
        public void LoadRep()
        {
            DataTable dt_AutoRecipe = new Catering_Rules.AutoRecipe().GetList("1=1 order by FoodID").Tables[0];
            this.Rep_AutoRecipe.DataSource = dt_AutoRecipe;
            this.Rep_AutoRecipe.DataBind();
        }


        protected void Rep_AutoRecipe_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.AutoRecipe().Delete(delID))
                { 
                    this.LoadRep();
                }
            }
        }

        //验证该类别是否存在
        public bool IsExists(int? FoodID,int? TypeID,int? TypeInfoID)
        {
            bool isExists = false;
            string strwhere = "FoodID="+FoodID+" and TypeID="+TypeID+" and TypeInfoID="+TypeInfoID;
            DataTable dt_AutoRecipe = new Catering_Rules.AutoRecipe().GetList(strwhere).Tables[0];
            if (dt_AutoRecipe != null && dt_AutoRecipe.Rows.Count > 0)
            {
                isExists = true;
            }
            return isExists;
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            Catering_Model.AutoRecipe md = new Catering_Model.AutoRecipe();
            md.id = new Catering_Rules.AutoRecipe().GetMaxId();
            md.FoodID = Convert.ToInt32(this.Drop_Food.SelectedValue);
            md.TypeID = 0; //类别合并为一个树结构 该字段已经没用了
            md.TypeInfoID = Convert.ToInt32(this.Drop_DishType.SelectedValue);
            md.Number = Convert.ToInt32(this.Drop_TypeNum.SelectedValue);

            DataTable Typedt = new Catering_Rules.DishType().GetList("ParentID=" + this.Drop_DishType.SelectedValue).Tables[0];
            if (Typedt != null && Typedt.Rows.Count > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择菜品分类根节点！');</script>");
                return;
            }

            if (IsExists(md.FoodID, md.TypeID, md.TypeInfoID))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该类别已存在！');</script>");
            }
            else
            {
                new Catering_Rules.AutoRecipe().Add(md);
                this.LoadRep();
            }
        }

        /// <summary>
        /// 获取菜品分类名称
        /// </summary>
        /// <param name="DishTypeID"></param>
        /// <returns></returns>
        public string GetDishTypeName(int DishTypeID)
        {
            string TypeName = "";
            if(DishTypeID==-1)
            {
                TypeName="全部";
            }
            else
            {
                Catering_Model.DishType md=new Catering_Rules.DishType().GetModel(DishTypeID);
                TypeName = md.DT;
                if (md.ParentID != 0)
                {
                    this.GetDishParentTypeName(md.ParentID, ref TypeName);
                }
            }
            return TypeName;
        }

        public string GetDishParentTypeName(int ParentID,ref string TypeName)
        {
            Catering_Model.DishType md = new Catering_Rules.DishType().GetModel(ParentID);
            TypeName = md.DT + "-" + TypeName;
            if (md.ParentID != 0)
            {
                this.GetDishParentTypeName(md.ParentID,ref TypeName);
            }
            return TypeName;
        }
    }
}